package cn.iocoder.yudao.module.famous.doctor.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.forumRecord.vo.FamForumRecordExportReqVO;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.reply.Enum.ReplyEnum;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.forumRecord.FamForumRecordDO;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.forumRecord.FamForumRecordMapper;
import cn.iocoder.yudao.module.famous.doctor.service.forumRecord.FamForumRecordServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author zhangjiasong
 * @description: TODO
 * @date 2023/11/14 18:47
 */
@Slf4j
@Component
public class FamReplyDOJob implements JobHandler {

    @Resource
    private FamForumRecordServiceImpl famForumRecordService;
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private FamForumRecordMapper famForumRecordMapper;

    static String REPLY_UPVOTEkey = "fam_forum_record" + ":" + ReplyEnum.REPLY_UPVOTE.getValue();
    static String REPLY_COLLECTkey = "fam_forum_record" + ":" + ReplyEnum.REPLY_COLLECT.getValue();

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String execute(String param) throws Exception {
        String[] parts = param.split("=");
        Long tenantid = Long.valueOf(parts[1]);

        //没有id 的标识需要添加数据库
        List<FamForumRecordDO> combinedList = new ArrayList<>();
        combinedList.addAll(redisTemplate.opsForHash().values(REPLY_UPVOTEkey));
        combinedList.addAll(redisTemplate.opsForHash().values(REPLY_COLLECTkey));
        if (combinedList.size()>0) {
            List<FamForumRecordDO> collect = combinedList.stream().filter(famForumRecordDO ->
                    famForumRecordDO.getId() == null
            ).collect(Collectors.toList());
            if (collect.size() > 0) {
                for (FamForumRecordDO famForumRecordDO : collect) {
                    famForumRecordDO.setTenantId(tenantid);
                    famForumRecordMapper.insert(famForumRecordDO);
                    redisTemplate.opsForHash().put("fam_forum_record" + ":" + famForumRecordDO.getState(), famForumRecordDO.getCreator() + "_" + famForumRecordDO.getForumId() + "_" + famForumRecordDO.getThumbsupId(), famForumRecordDO);
                }
            }

            log.info(String.format("数据库添加  %s 个", collect.size()));
        }



        //数据库数据存入redis
        List<FamForumRecordDO> famReplyList = famForumRecordService.getFamForumRecordList(new FamForumRecordExportReqVO());
        for (FamForumRecordDO createReqVO : famReplyList) {
            if (createReqVO.getState() == ReplyEnum.REPLY_UPVOTE.getValue()) {
                String hashkey = createReqVO.getCreator() + "_" + createReqVO.getForumId() + "_" + createReqVO.getThumbsupId();
                redisTemplate.opsForHash().put(REPLY_UPVOTEkey, hashkey, createReqVO);
            } else {
                String hashkey = createReqVO.getCreator() + "_" + createReqVO.getForumId() + "_" + createReqVO.getThumbsupId();
                redisTemplate.opsForHash().put(REPLY_COLLECTkey, hashkey, createReqVO);
            }
        }
        log.info(String.format("redis更新 %s 个", famReplyList.size()));

        return null;
    }
}
